/*----------------------------------------------------------*/
/* XMLHTTPRequest Enable                                    */
/*----------------------------------------------------------*/
function createObject() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

var http=createObject();
/*----------------------------------------------------------*/
/* Login                                                    */
/*----------------------------------------------------------*/
var nocache=0;
function login() {
    document.getElementById('login_response').innerHTML="Loading...";
    var username = encodeURI(document.getElementById('username').value);
    var password = encodeURI(document.getElementById('password').value);
    nocache=Math.random();
    http.open('GET', './phpScripts/checklogin.php?username='+username+'&password='+password+'&nocache='+nocache);
    http.onreadystatechange=loginReply;
    http.send(null);
}
function loginReply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        if(response == 0) {//if login fails
            document.getElementById('login_response').innerHTML=
                'Login failed! Verify user and password';
        }
        else {//successful login
            window.location="index.php";
        }
    }
}
/*----------------------------------------------------------*/
/* Register                                                 */
/*----------------------------------------------------------*/
function register() {
    if(validate()) {
        
    }
}
function validate() {
    var valid = true;
    var x=document.forms["registration_form"]["email"].value;
    //verify email
    if(x==null || x=="") {
        valid = false;
        document.getElementById('email_error').innerHTML=
            'Error! Email must be provided';
    }
    else { 
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
            valid = false;
            document.getElementById('email_error').innerHTML=
            'Error! Not a valid e-mail address';
        }
        else document.getElementById('email_error').innerHTML="";
    }
    //verify name
    x=document.forms["registration_form"]["fName"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('fName_error').innerHTML=
            'Error! First name must be provided';
    }
    else document.getElementById('fName_error').innerHTML="";
    x=document.forms["registration_form"]["lName"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('lName_error').innerHTML=
            'Error! Last name must be provided';
    }
    else document.getElementById('lName_error').innerHTML="";
    //verify user type
    x=document.forms["registration_form"]["uType"].value;
    if(x==null) {
        valid = false;
        document.getElementById('uType_error').innerHTML=
            'Error! User type must be provided';
    }
    else document.getElementById('uType_error').innerHTML="";
    //verify password
    x=document.forms["registration_form"]["pass1"].value;
    var y=document.forms["registration_form"]["pass2"].value;
    if(x==null || x=="" || y==null || y=="") {
        valid = false;
        document.getElementById('password_error').innerHTML=
            'Error! Password must be provided';
    }
    else if(x!=y) {
        valid = false;
        document.getElementById('password_error').innerHTML=
            'Error! Passwords do not match';
    }
    else document.getElementById('password_error').innerHTML="";
    //return verification status
    return valid; 
}